<?php

/**
 *   OPENSHOP 管理中心商品季节管理
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: season.php,v 1.0 2009/08/19 08:36:58 modified $
 *   @author      Jiajun <jiajun@baison.com.cn>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

$exc = new exchange($os->table("season"), $db, 'season_id', 'season_name');

/*------------------------------------------------------ */
//-- 季节列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	
	//检查权限
	admin_priv('season_view');
		
    $smarty->assign('ur_here',      '商品季节');
    $smarty->assign('action_link',  array('text' => '添加季节', 'href' => 'season.php?act=add'));
    $smarty->assign('full_page',    1);

    $season_list = get_seasonlist();

    $smarty->assign('season_list',  $season_list['season']);
    $smarty->assign('filter',       $season_list['filter']);
    $smarty->assign('record_count', $season_list['record_count']);
    $smarty->assign('page_count',   $season_list['page_count']);

    assign_query_info();
    
    $smarty->display('season_list.tpl');
}

/*------------------------------------------------------ */
//-- 添加季节
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('season_add');

    $smarty->assign('ur_here',     '添加商品季节');
    $smarty->assign('action_link', array('text' => '商品季节', 'href' => 'season.php?act=list'));
    $smarty->assign('form_action', 'insert');

    assign_query_info();
    
    $smarty->display('season_info.tpl');
}

elseif ($_REQUEST['act'] == 'insert')
{
    /*检查季节名是否重复*/
    admin_priv('season_add');

    $is_only = $exc->is_only('season_name', $_POST['season_name']);

    if (!$is_only)
    {
        sys_msg(sprintf('季节名称 %s 已经存在！', stripslashes($_POST['season_name'])), 1);
    }

    $is_only = $exc->is_only('season_code', $_POST['season_code']);

    if (!$is_only)
    {
        sys_msg(sprintf('季节代码 %s 已经存在！', stripslashes($_POST['season_code'])), 1);
    }
    
    /*插入数据*/

    $sql = "INSERT INTO ".$os->table('season')."(season_name, season_code) ".
           "VALUES ('$_POST[season_name]', '$_POST[season_code]')";
    $db->query($sql);

    admin_log($_POST['season_name'],'add','season');

    /* 清除缓存 */
    clear_cache_files();

    $link[0]['text'] = '继续添加新商品季节';
    $link[0]['href'] = 'season.php?act=add';

    $link[1]['text'] = '返回商品季节列表';
    $link[1]['href'] = 'season.php?act=list';

    sys_msg('新商品季节添加成功！', 0, $link);
}

/*------------------------------------------------------ */
//-- 编辑季节
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('season_edit');
    
    $sql = "SELECT season_id, season_name, season_code ".
            "FROM " .$os->table('season'). " WHERE season_id='$_REQUEST[id]'";
    $season = $db->GetRow($sql);

    $smarty->assign('ur_here',     '编辑季节记录');
    $smarty->assign('action_link', array('text' => '商品季节', 'href' => 'season.php?act=list&' . list_link_postfix()));
    $smarty->assign('season',       $season);
    $smarty->assign('form_action', 'updata');
	
    assign_query_info();
    
    $smarty->display('season_info.tpl');
}
elseif ($_REQUEST['act'] == 'updata')
{
    admin_priv('season_edit');
    
    if ($_POST['season_name'] != $_POST['old_seasonname'])
    {
        /*检查季节名是否相同*/
        $is_only = $exc->is_only('season_name', $_POST['season_name'], $_POST['id']);

        if (!$is_only)
        {
            sys_msg(sprintf('季节名称 %s 已经存在！', stripslashes($_POST['season_name'])), 1);
        }
    }
    
    if ($_POST['season_code'] != $_POST['old_seasoncode'])
    {
        /*检查季节代码是否相同*/
        $is_only = $exc->is_only('season_code', $_POST['season_code'], $_POST['id']);

        if (!$is_only)
        {
            sys_msg(sprintf('季节代码 %s 已经存在！', stripslashes($_POST['season_code'])), 1);
        }
    }    

    $param = "season_name = '$_POST[season_name]', season_code='$_POST[season_code]'";

    if ($exc->edit($param,  $_POST['id']))
    {
        /* 清除缓存 */
        clear_cache_files();

        admin_log($_POST['season_name'], 'edit', 'season');

        $link[0]['text'] = '返回季节列表';
        $link[0]['href'] = 'season.php?act=list&' . list_link_postfix();
        $note = vsprintf('季节 %s 修改成功！', $_POST['season_name']);
        sys_msg($note, 0, $link);
    }
    else
    {
        die($db->error());
    }
}

/*------------------------------------------------------ */
//-- 删除季节
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('season_del');

    $id = intval($_GET['id']);

    /* 删除该季节 */
    $sql = "SELECT season_id FROM " .$os->table('season'). " WHERE season_id = '$id'";
    $season_id = $db->getOne($sql);
    if ($season_id > 0)
    {
	    $exc->drop($id);
    }
    $url = 'season.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    os_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
	//检查权限
	admin_priv('season_view');
	
    $season_list = get_seasonlist();

    $smarty->assign('season_list',  $season_list['season']);
    $smarty->assign('filter',       $season_list['filter']);
    $smarty->assign('record_count', $season_list['record_count']);
    $smarty->assign('page_count',   $season_list['page_count']);

    make_json_result($smarty->fetch('season_list.tpl'), '',
        array('filter' => $season_list['filter'], 'page_count' => $season_list['page_count']));
}

/**
 * 获取季节列表
 *
 * @access  public
 * @return  array
 */
function get_seasonlist()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['os']->table('season');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT * FROM ".$GLOBALS['os']->table('season')." ORDER BY season_id ASC";

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $rows;
    }

    return array('season' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
?>